from sqlite3 import DatabaseError
import pymysql
DBCONFIG = {
    "host":"119.45.233.102",
    "port":3306,
    "user":"testgoup",
    "pswd":"1qaz!QAZ",
    "dabs":"haimo"
}
class MysqlTools():
    def __init__(self, host=DBCONFIG["host"], port=DBCONFIG["port"],user=DBCONFIG["user"],pswd=DBCONFIG["pswd"],dabs=DBCONFIG["dabs"]):
        self.dabs = dabs  #12-15行代码是传参,创建成员变量，为了让其在类中共用
        self.host = host
        self.user = user
        self.port = port
        self.pswd = pswd
        self.connect = self.get_db_connect()
# 创建数据库链接：连接数据库
    def get_db_connect(self):
        return pymysql.connect(host=self.host, port=self.port, user=self.user,pswd=self.pswd,dabs=self.dabs)

 # 获取游标
def get_db_cursor(self):
    return self.connect.cursor()
# 执行sql语句
def query(self, sql):
    cur = self.get_db_cursor()#获取游标：查询界面
    cur.execute(sql)
    return cur.fetchall()  #获取查询结果
 # 析构方法：关闭数据连接，释放资源
def __del__(self):
    self.connect.close()
if __name__ == "__main__":
    sql = "select * from tb_system_user where code = 'SAS201801'"
    result = MysqlTools().query(sql)
    print(result)

# sql = "select * from tb_system_user where code = 'SAS201801'" # 字符串
 # # 1连接并且操作数据库
 # db = pymysql.connect(host=DBCONFIG["host"], 
#                 port=DBCONFIG["port"], 
 #                 user=DBCONFIG["user"],
 #                 password=DBCONFIG["password"],
 #                 db=DBCONFIG["db"])
# # 2.获取游标：查询窗口
# cur = db.cursor()
# # 3.执行sql查询语句
# cur.execute(sql)
# # 4.获取结果
# res = cur.fetchall()
# # 关闭数据连接
# db.close()
 # print(res)